package com.yunao.service.impl;

import com.yunao.commons.utils.ResultVOUtil;
import com.yunao.commons.vo.ResultVO;
import com.yunao.dao.PositionRepository;
import com.yunao.entity.Position;
import com.yunao.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PositionServiceImpl implements PositionService {
    @Autowired
    private PositionRepository positionRepository;

    @Override
    public List<Position> findByParentid(Integer parentid) {
        return positionRepository.findByParentid(parentid);
    }

    @Cacheable(cacheNames = "position",key = "123")
    @Override
    public ResultVO finAll() {
        Map<String, List<String>> map = new HashMap();
        List<Position> firstLevels = positionRepository.findByParentid(0);
        for (Position position:firstLevels) {
            List<Position> secondLevels = positionRepository.findByParentid(position.getId());
            List<String> citys = new ArrayList<>(64);
            for (Position secondlevel:secondLevels) {
                citys.add(secondlevel.getName());
            }
            map.put(position.getName(),citys);
        }
        return ResultVOUtil.success(map);
    }
}
